package com.lx.forum.exception;

import com.lx.forum.common.AppResult;
import com.lx.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 李夏
 * @version 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult applicationException(ApplicationException e){
        log.info(e.getMessage());
        if(e.getErrResult()!=null){
            return e.getErrResult();
        }
        if(e.getMessage()==null||e.getErrResult().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());

    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult exception(Exception e){
        log.info(e.getMessage());
        if(e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());
    }
}
